#include <fstream>
#include <string>
#include <iostream>
#include <time.h>
#include "html.h"
#include <sstream>

using namespace std;


string int2Str(int number)
{
    string temp;
    stringstream strStream;
    strStream << number;
    temp = strStream.str();
    return temp;
}

string char2Str(char number[])
{
       string str;
       for(int i = 0; number[i] != 0; i++)
       str += number[i];
       return str;
}

void startTag(string backgroundUrl)
{
     makeCss("style.css");
     addCss("style.css","#cell{text-align:center;color:#FFD85C;}");
     addCss("style.css","#cellTop{text-align:center;color:#FFF;}");
     addCss("style.css","#approved{text-align:center;font-weight:bold;color:#4DFF5C;}");
     addCss("style.css","#rejected{text-align:center;font-weight:bold;color:#FF0000;}");
     addCss("style.css","#title{font-family:Verdana;color:#FFF;}");
     addCss("style.css","#open{text-align:center;font-weight:bold;color:#FF97FF;}");
     addCss("style.css","#withdraw{text-align:center;font-weight:bold;color:#C6FFFF;}");
     addCss("style.css","#deposit{text-align:center;font-weight:bold;color:#64FF1D;}");
     addCss("style.css","#check{text-align:center;font-weight:bold;color:#38EAD2;}");
     addCss("style.css","body{background-image:url('" + backgroundUrl + "');}");
     addCss("style.css","#baseL{text-align:left;color:#FFF;}");
     addCss("style.css","#baseR{text-align:right;color:#FFCE00}");
     addCss("style.css","a{padding-left:5px;padding-right:5px; text-decoration:none;color:#FFF;}");
     addCss("style.css","a:hover{background-color:#97FFCB;}");
     addCss("style.css","#open{text-align:center;font-weight:bold;color:#FF0000;}");
     addCss("style.css","#mAd{text-align:center;font-weight:bold;color:#FF0000;}");
     stTag("Mars Bank Log","style.css");
     anchorAdd("top");
     pAdd("","","","");
     hAdd(3,"Mars Bank Log","","title","");
     hrAdd();
     tableAdd(2,910,"","","");
             rowAdd();
                   tdAdd("Date",110,0,0,"","cellTop","");
                   tdAdd("Time",110,0,0,"","cellTop","");
                   tdAdd("Account Number",240,0,0,"","cellTop","");
                   tdAdd("Action",150,0,0,"","cellTop","");
                   tdAdd("Amount",150,0,0,"","cellTop","");
                   tdAdd("Result",150,0,0,"","cellTop","");
             rowClose();
     tableClose();
     addTag();


}

void logHandler(int accNum, int action, int amount, bool result, string bg)
{
     string actionStr, resultStr, actionId, resultId, amountStr;
     if(amount == 0)
       amountStr = "N/A";
     else
       amountStr = "$" + int2Str(amount);
     switch(action)
     {
                   case 0:
                        actionStr = "Open Account";
                        actionId = "open";
                        break;
                   case 1:
                        actionStr = "Check Balance";
                        actionId = "check";
                        break;
                   case 2:
                        actionStr = "Withdraw";
                        actionId = "withdraw";
                        break;
                   case 3:
                        actionStr = "Deposit";
                        actionId = "deposit";
                        break;
                   case 4:
                        actionStr = "Authenticate";
                        actionId = "open";
                        break;
                   case 5:
                        actionStr = "mAd with us";
                        actionId = "mAd";
                        break;
                   default:
                           actionStr = "UNKNOWN";
                           actionId = "UNKNOWN";
     }
     switch(result)
     {
                   case true:
                        resultStr = "Approved";
                        resultId = "approved";
                        break;
                   case false:
                        resultStr = "Rejected";
                        resultId = "rejected";
                        break;
                   default:
                        resultStr = "UNKNOWN";
                        resultId = "UNKNOWN";
     }
     string msg = "File auto generated by CACC ATM v2.1 (SLib version:1.0.1)";
     ifstream in;
     in.open("log.html");
     if(!in)
     {
           in.close();
           startTag(bg);
     }
     in.close();
     rmTag();
     char dateStr [9]; char timeStr [9];
     _strdate( dateStr ); _strtime( timeStr );
     brAdd();
     tableAdd(2,910,"","","");
             rowAdd();
                   tdAdd(char2Str(dateStr),110,0,0,"","cell","");
                   tdAdd(char2Str(timeStr),110,0,0,"","cell","");
                   tdAdd(int2Str(accNum),240,0,0,"","cell","");
                   tdAdd(actionStr,150,0,0,"",actionId,"");
                   tdAdd(amountStr,150,0,0,"","cellTop","");
                   tdAdd(resultStr,150,0,0,"",resultId,"");
             rowClose();
     tableClose();
     tableAdd(0,910,"","","");
             rowAdd();
                   tdAdd(msg,455,0,0,"","baseL","");
                   tdAdd("<a href=\"#top\">Back to top ^</a>",455,0,0,"","baseR","");
             rowClose();
     tableClose();
}
